<template>
  <div class="content">
    <div class="header_box">
      <div class="header_left">问题IP报告</div>
      <div class="header_right">
        <el-form :inline="true" :model="searFrom" class="form_box">
          <el-form-item>
            <el-input
              v-model="searFrom.name"
              placeholder="IP地址"
              style="width: 200px"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="searFrom.code"
              placeholder="产品标签"
              style="width: 200px"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="init">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="reportVis = true"
              >报告问题</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ background: '#fafbfc', color: '#bbbcc3' }"
      max-height="500"
    >
      <el-table-column type="index" :index="indexMethod" />
      <el-table-column prop="ip" label="IP地址" />
      <el-table-column prop="productTag" label="产品标签" />
      <el-table-column prop="reportReason" label="报告原因" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <div class="icon_content">
            <el-tooltip
              class="box-item"
              effect="dark"
              content="编辑"
              placement="top-start"
            >
              <div class="icon_box">
                <el-icon @click="edit(row)" size="20" color="#71778d"
                  ><EditPen
                /></el-icon>
              </div>
            </el-tooltip>
            <el-tooltip
              class="box-item"
              effect="dark"
              content="删除"
              placement="top-start"
            >
              <div class="icon_box_d">
                <el-icon @click="remove(row)" size="20" color="#71778d"
                  ><Remove
                /></el-icon>
              </div>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page_box">
      <el-pagination
        v-model:current-page="searFrom.pageNo"
        v-model:page-size="searFrom.pageSize"
        :page-sizes="[10, 50, 100, 150]"
        :small="small"
        :disabled="false"
        :background="true"
        layout="sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 报告对话框 -->
    <el-dialog v-model="reportVis" title="问题IP报告" width="70%">
      <el-form label-position="left" label-width="200px" :model="objectForm">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="涉及IP">
              <el-input v-model="objectForm.ip" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="涉及产品标签">
              <el-input v-model="objectForm.productTag" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报告原因">
              <el-input v-model="objectForm.reportReason" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="备注">
              <el-input v-model="objectForm.notes" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="reportVis = false">取消</el-button>
          <el-button type="primary" @click="confirmAdd"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from "vue";
import * as tenantApi from "../../api/project1/ip/ip.js";
import { ElMessage } from "element-plus";
import confirm from "../../utils/ElMessageUtils.js";

// 示例数据
const exampleData = [
  {
    ip: "192.168.1.1",
    productTag: "标签A",
    reportReason: "网络不稳定",
    notes: "需要进一步调查",
  },
  {
    ip: "10.0.0.1",
    productTag: "标签B",
    reportReason: "连接超时",
    notes: "可能是路由问题",
  },
];

const tableData = ref([]);

const objectForm = ref({
  ip: "",
  productTag: "",
  reportReason: "",
  notes: "",
});

let searFrom = ref({
  pageNo: 1,
  pageSize: 10,
});

let total = ref(0);

let reportVis = ref(false);

const init = async () => {
  try {
    // 模拟API请求失败，使用示例数据
    tableData.value = exampleData;
    total.value = exampleData.length;
  } catch (error) {
    console.error("API请求失败:", error);
    ElMessage.error("无法加载数据，请稍后重试");
  }
};

const confirmAdd = async () => {
  const res = await tenantApi.saveOrUpdate(objectForm.value);
  const { code, msg } = res;
  if (code == "200") {
    ElMessage.success("操作成功");
    reportVis.value = false;
    init();
    return;
  }
  ElMessage.success(msg);
};

const remove = async (row) => {
  const b = await confirm("您确定要删除吗？");
  if (!b) {
    return;
  }
  const res = await tenantApi.remove({ id: row.id });
  const { code, msg } = res;
  if (code == "200") {
    ElMessage.success("操作成功");
    init();
    return;
  }
  ElMessage.success(msg);
};

const edit = (row) => {
  objectForm.value = { ...row };
  reportVis.value = true;
};

onBeforeMount(() => {
  init();
});
</script>

<style lang="less" scoped>
.header_right {
  background-color: #ffffff;
  justify-content: flex-end;
  border-radius: 0px 15px 0px 0px;
}
:deep(.form_box) {
  width: 80%;
  height: 40%;
  margin: 0px;
  display: flex;
  justify-content: flex-end;
}
.header_left {
  border-radius: 15px 0px 0px 0px;
  font-weight: 700;
}
</style>